/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

/****************************************************************************/
/***                                                                      ***/
/***   (C) 2005 Dierk Ohlerich, all rights reserved                       ***/
/***                                                                      ***/
/****************************************************************************/

level11;

asc
{
  cbuffer TestMtrlVSPara : register(c0) : slot vs 0
  {
    float4 uvoffset;
    row_major float4x4 mvp;
    float4 ldir;
  };
  cbuffer TestMtrlGSPara : register(c0) : slot gs 0
  {
    float width;
  };
}


material TestMtrl 
{
  // the vertex shader

  vs
  {    
    asc vs_4_0                 // hlsl code
    {
      sampler2D s0 : register(s0);
      use TestMtrlVSPara;

      void main
      (
        in float3 in_pos : POSITION,            // input, from vertex array
        in float3 in_norm : NORMAL,
        in float2 in_uv : TEXCOORD0,
        in uint in_id : SV_VERTEXID,
        out float4 out_pos : POSITION,
        out float4 out_col : COLOR,
        out uint out_id : ID,
      ) 
      {
        float d = tex2Dlod(s0,float4(in_uv+uvoffset.xy,0,0)).x
                + tex2Dlod(s0,float4(in_uv+uvoffset.zw,0,0)).x;
        float3 pos = in_pos+in_norm*(d-1)*0.2;
        out_pos = mul(float4(pos,1),mvp);
        out_id = in_id;

        float l = saturate(dot(in_norm,ldir.xyz))*0.75+0.25;
        out_col = float4(l,l,l,1);
      }
    }
  }

  // geometry shader

  gs
  {
    asc gs_4_0
    {
      use TestMtrlGSPara;

      struct tvin
      {
        float4 pos : POSITION;
        float4 col : COLOR;
        uint id : ID;
      };
      struct tvout 
      {
        float4 col : COLOR0;
        float4 pos : POSITION;
      };

      [maxvertexcount(12)]
      void main
      (
        triangle tvin vin[3],
        inout TriangleStream<tvout> vouts,
      )
      {
        tvout vout;

        int j = 2;
        for(int i=0;i<3;i++)
        {
          if(vin[i].id < vin[j].id)
          {
            float4 da = float4(normalize(vin[i].pos.xy-vin[j].pos.xy)*width,0,0);
            float4 db = float4(da.y,-da.x,0,0);
            vout.col = vin[i].col;
            vout.pos = vin[i].pos+da+db;
            vouts.Append(vout);
            vout.pos = vin[i].pos+da-db;
            vouts.Append(vout);
            vout.col = vin[j].col;
            vout.pos = vin[j].pos-da+db;
            vouts.Append(vout);
            vout.pos = vin[j].pos-da-db;
            vouts.Append(vout);
            
            vouts.RestartStrip();
            
          }
          j = i;
        }
      }
    }
  }

  // the pixel shader

  ps
  {

    asc ps_4_0
    {
      void main
      (
        in float4 col : COLOR0,
        out float4 result : COLOR0
      )
      {
        result = col;
      }
    }
  }
};

  